#include "auth.h"

AuthDialog::AuthDialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("Login / Register");

    QTabWidget *tabWidget = new QTabWidget(this);
    QWidget *loginTab = createLoginTab();
    QWidget *registerTab = createRegisterTab();

    tabWidget->addTab(loginTab, "Login");
    tabWidget->addTab(registerTab, "Register");

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(tabWidget);
    setLayout(mainLayout);
}

QWidget* AuthDialog::createLoginTab()
{
    QWidget *loginTab = new QWidget;

    QFormLayout *formLayout = new QFormLayout;
    QLineEdit *usernameEdit = new QLineEdit;
    QLineEdit *passwordEdit = new QLineEdit;

    passwordEdit->setEchoMode(QLineEdit::Password);

    formLayout->addRow("Username:", usernameEdit);
    formLayout->addRow("Password:", passwordEdit);

    // 添加单选框
    QHBoxLayout *radioButtonLayout = new QHBoxLayout;
    QRadioButton *studentRadio = new QRadioButton("学生");
    QRadioButton *teacherRadio = new QRadioButton("教师");
    QRadioButton *supervisorRadio = new QRadioButton("督导");

    // 将单选框添加到水平布局中
    radioButtonLayout->addWidget(studentRadio);
    radioButtonLayout->addWidget(teacherRadio);
    radioButtonLayout->addWidget(supervisorRadio);

    // 将单选框布局添加到整体布局中
    formLayout->addRow("Identity:", radioButtonLayout);

    QPushButton *loginButton = new QPushButton("Login");

    connect(loginButton, &QPushButton::clicked, [=]() {
        QString username = usernameEdit->text();
        QString password = passwordEdit->text();
        QString userType;

        if (studentRadio->isChecked()) {
            userType = "student";
        } else if (teacherRadio->isChecked()) {
            userType = "teacher";
        } else if (supervisorRadio->isChecked()) {
            userType = "supervisor";
        }

        handleLogin(username, password, userType);
    });

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addLayout(formLayout);
    layout->addWidget(loginButton);
    loginTab->setLayout(layout);

    return loginTab;
}

QWidget* AuthDialog::createRegisterTab()
{
    QWidget *registerTab = new QWidget;

    QFormLayout *formLayout = new QFormLayout;
    QLineEdit *usernameEdit = new QLineEdit;
    QLineEdit *passwordEdit = new QLineEdit;
    QLineEdit *confirmPasswordEdit = new QLineEdit;

    passwordEdit->setEchoMode(QLineEdit::Password);
    confirmPasswordEdit->setEchoMode(QLineEdit::Password);

    formLayout->addRow("Username:", usernameEdit);
    formLayout->addRow("Password:", passwordEdit);
    formLayout->addRow("Confirm Password:", confirmPasswordEdit);

    QPushButton *registerButton = new QPushButton("Register");

    connect(registerButton, &QPushButton::clicked, [=]() {
        QString username = usernameEdit->text();
        QString password = passwordEdit->text();
        QString confirmPassword = confirmPasswordEdit->text();
        handleRegister(username, password, confirmPassword);
    });

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addLayout(formLayout);
    layout->addWidget(registerButton);
    registerTab->setLayout(layout);

    return registerTab;
}

void AuthDialog::handleLogin(const QString &username, const QString &password, const QString &userType)
{
    SqlHelper &dbHelper = SqlHelper::getInstance();
    QList<QVariantList> users = dbHelper.queryUsers();

    for (const auto &user : users)
    {
        if (user[0].toString() == username
         && user[1].toString() == password
         && user[2].toString() == userType)
        {
            UserSession::getInstance().loginUser(username, userType);
            QMessageBox::information(this, "Success", "Login Successful!");
            accept();
            return;
        }
    }

    QMessageBox::warning(this, "Error", "Invalid username, password or user type.");
}

void AuthDialog::handleRegister(const QString &username, const QString &password, const QString &confirmPassword)
{
    if (password != confirmPassword) {
        QMessageBox::warning(this, "Error", "Passwords do not match.");
        return;
    }

    SqlHelper &dbHelper = SqlHelper::getInstance();

    if (dbHelper.insertUser(username, password, "user")) {
        QMessageBox::information(this, "Success", "Registration Successful!");
    } else {
        QMessageBox::warning(this, "Error", "Registration failed. Username might be taken.");
    }
}
